// pages/wrong/wrong.js
let num=0;
let db =  wx.cloud.database()
Page({


    data: {
        questList:[],
        total:0,
        page:0,
        answer:[],
        answerAll:[],

    },


    onLoad: function (options) {
        num=0;
        this.setData({
          page:num  
        })
        this.collQuestionList()
        
    },
    onShow(){
        
        this.collQuestionList()
    },
    before(){
        if(num > 0 ){
            num--;
        this.setData({
            page:num
        })
  
        }
    },
    next(){
        if(num < this.data.total-1){
            num++;
        this.setData({
            page:num
        })
        this.collQuestionList()
  
        }else{
            wx.showToast({
                icon:'none',
                title: '已经没有题了',
              })
        }

    },
     //接收查询题集
     async collQuestionList(){   
        let user = wx.getStorageSync('userInfo')
        let {data} =  await db.collection('collection')
        .where({
              _openid:user._openid
         }).get()

         this.setData({
               total:data.length
         })

          this.setData({
             questList:data,
         })
        console.log("信息",data)
        if(this.data.total<=0){
            return
        }
         let answer =data[num].questionall.answer

         this.data.answer[num]={selected:answer.toString().substring(0,1)}
         this.setData({
            answerAll:this.data.answer
         })
    },
    async  deleArea(){
        console.log(this.data.questList[num]._id)
        let id = this.data.questList[num]._id
        let that =this;
         await  wx.showModal({
            title: '删除',
            content: '是否删除此题',
            success (res) {
              if (res.confirm) {
                  db.collection('collection')
                  .doc(id)
                  .remove()
                  .then(ress =>{
                    //   console.log("成功了",ress)
                    // console.log(num,that.data.total)

                      if(num+1 == that.data.total){
                          that.onLoad()
                      }else{
                          that.collQuestionList()
                      }
                      wx.setStorageSync('coll', null)
                    })
                } else if (res.cancel) {
                // console.log('用户点击取消')
              }
            }
          })
    }   
   
})